package com.wswy.wzcx.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import com.wswy.wzcx.R;


public class SideBar extends View {
  private Paint paint;

  private boolean showBackground;

  public static String[] letters = {"#","A", "B", "C", "D", "E", "F", "G", "H",
      "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
      "V", "W", "X", "Y", "Z"};

  private OnChooseLetterChangedListener onChooseLetterChangedListener;

  public SideBar(Context context) {
    this(context, null);

  }

  public SideBar(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public SideBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    bgColor = Color.parseColor("#BFBFBF");

    TextView textView = new TextView(context);
    textView.setTextSize(12);
    textView.setTextColor(ContextCompat.getColor(context,R.color.textColorSecondary));
    paint = textView.getPaint();

  }

  private int bgColor;

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (showBackground) {
      // 字母条背后颜色
      canvas.drawColor(bgColor);
    }
    int height = getHeight();
    int width = getWidth();
    // 平均每个字母占的高度
    int singleHeight = height / letters.length;
    for (int i = 0; i < letters.length; i++) {
      // 字母的颜色
      float x = width / 2 - paint.measureText(letters[i]) / 2;
      float y = singleHeight * i + singleHeight;
      canvas.drawText(letters[i], x, y, paint);
    }
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
    int action = event.getAction();
    float y = event.getY();
    int c = (int) (y / getHeight() * letters.length);
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        showBackground = true;
        if (c > -1 && c < letters.length) {
          getParent().requestDisallowInterceptTouchEvent(true);
          if (onChooseLetterChangedListener != null) {
            onChooseLetterChangedListener.onChooseLetter(letters[c]);
          }
        }
        invalidate();
        break;
      case MotionEvent.ACTION_MOVE:
        if (c > -1 && c < letters.length && onChooseLetterChangedListener != null) {
          onChooseLetterChangedListener.onChooseLetter(letters[c]);
        }
        break;
      case MotionEvent.ACTION_UP:
        showBackground = false;
        getParent().requestDisallowInterceptTouchEvent(false);
        if (onChooseLetterChangedListener != null) {
          onChooseLetterChangedListener.onNoChooseLetter();
        }
        invalidate();
        break;
    }
    return true;
  }


  public void setOnTouchingLetterChangedListener(OnChooseLetterChangedListener onChooseLetterChangedListener) {
    this.onChooseLetterChangedListener = onChooseLetterChangedListener;
  }

  public interface OnChooseLetterChangedListener {

    void onChooseLetter(String s);

    void onNoChooseLetter();

  }
}
